#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UTable.Objects.Controls.Primitives;
using System.Windows;
using System.Windows.Threading;
using System.Threading;

namespace UTable.Objects.Controls
{
	public class UDialog: UPopup
	{
		#region Private Fields

		

		#endregion


		#region Constructors

		static UDialog()
        {
			DefaultStyleKeyProperty.OverrideMetadata(typeof(UDialog), new FrameworkPropertyMetadata(typeof(UDialog)));
        }

		public UDialog()
		{

		}

		#endregion

		#region Properties


		/// <summary>
		/// Indicates whether the dialog hold the focus all the time. 
		/// If true, then only the dialog in the object can be interacted with
		/// </summary>
		public bool HoldFocus
		{
			get { return (bool)GetValue(HoldFocusProperty); }
			set { SetValue(HoldFocusProperty, value); }
		}

		public static readonly DependencyProperty HoldFocusProperty =
			DependencyProperty.Register("HoldFocus", typeof(bool), typeof(UDialog), new UIPropertyMetadata(true));

		protected UDialogResult Result { get; set; }

		#endregion

		#region Public Methods

		protected void Close(UDialogResult result)
		{
			this.IsOpen = false;
			(UTableHelper.CurrentVisualTree as UObject).FocusOnPopup = false;

			if (Closed != null)
			{
				Closed(this, result);
			}
		}

		/// <summary>
		/// Open the dialog
		/// </summary>
		public UDialogResult Open()
		{
			IsOpen = true;
			if (HoldFocus)
			{
				(UTableHelper.CurrentVisualTree as UObject).FocusOnPopup = true;
			}

			// TODO: simulate the do modal 
			// this.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
			// Thread.Sleep(System.Threading.Timeout.Infinite);
			return Result;
		}

		/// <summary>
		/// Close the dialog
		/// </summary>
		public void Close()
		{
			Close(UDialogResult.Empty);
		}

		#endregion

		#region Events

		public new event UDialogClosedEventHandler Closed;

		#endregion

	}

	public delegate void UDialogClosedEventHandler(UDialog sender, UDialogResult result);

	public class UDialogResult
	{
		public static UDialogResult Empty = new UDialogResult();
	}
}
